#pragma once

#include "my_defs.h"
#include "my_mem.h"
#include "my_spinlock.h"

typedef void (*my_cleanup_handler_pt)(void *data);

typedef struct my_cleanup_item_s my_cleanup_item_t;

struct my_cleanup_item_s {
    my_cleanup_handler_pt       handler;
    void                        *data;
    my_cleanup_item_t           *next;
};

typedef struct {
    my_spinlock_t               lock;
    my_cleanup_item_t           head;
} my_cleanup_list_t;

extern my_cleanup_list_t        g_cleanup_list;

void my_cleanup_list_init(my_cleanup_list_t *list);

void my_cleanup_handler_add(my_cleanup_list_t *list,
    my_cleanup_handler_pt handler, void *data);

void my_cleanup_list_exec(my_cleanup_list_t *list);